/**
 * 
 */
package trungsi.gea.photos.dao;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Repository;

import trungsi.gea.photos.domain.Folder;
import trungsi.gea.photos.domain.Photo;

/**
 * @author trungsi
 *
 */
@Repository
public class PhotoDao extends GenericsDaoImpl<String, Photo> {

	@SuppressWarnings("unchecked")
	public List<Photo> findByFolder(final Folder folder) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {

			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setFilter("folder == folderId");
				query.setOrdering("createdDate");
				query.declareParameters("String folderId");
				
				return (List<Photo>) query.execute(folder.getId());
			}});
	}

	@SuppressWarnings("unchecked")
	public List<Photo> findByFolder(final Folder folder, final Date date) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {
			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setFilter("folder == folderId && createdDate > adate");
				query.setOrdering("createdDate");
				query.declareParameters("String folderId,java.util.Date adate");
				
				return (List<Photo>) query.execute(folder.getId(), date);
			}
		});
	}
	
	public int countByFolder(final Folder folder) {
		return getJdoTemplate().execute(new JdoCallback<Integer>() {
			@Override
			public Integer doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery("select count(this) from " + Photo.class.getName() + " where folder == folderId PARAMETERS String folderId");
				
				return (Integer) query.execute(folder.getId());
			}});
	}
	
	@SuppressWarnings("unchecked")
	public List<Photo> findByFolder(final Folder folder, final int limit) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {
			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setFilter("folder == folderId");
				query.setOrdering("createdDate");
				query.setRange(0, limit);
				query.declareParameters("String folderId");
				
				return (List<Photo>) query.execute(folder.getId());
			}});
	}
	
	@SuppressWarnings("unchecked")
	public List<Photo> findByFolder(final Folder folder, final Date date, final int limit) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {
			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setFilter("folder == folderId && createdDate > adate");
				query.setOrdering("createdDate");
				query.setRange(0, limit);
				query.declareParameters("String folderId,java.util.Date adate");
				
				return (List<Photo>) query.execute(folder.getId(), date);
			}});
	}

	@SuppressWarnings("unchecked")
	public List<Photo> findAll(final int limit) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {
			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setOrdering("createdDate");
				query.setRange(0, limit);
				
				return (List<Photo>) query.execute();
			}});
	}

	@SuppressWarnings("unchecked")
	public List<Photo> findAll(final Date date, final int limit) {
		return getJdoTemplate().execute(new JdoCallback<List<Photo>>() {
			@Override
			public List<Photo> doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery(Photo.class);
				query.setFilter("createdDate > adate");
				query.setOrdering("createdDate");
				query.setRange(0, limit);
				query.declareParameters("java.util.Date adate");
				
				return (List<Photo>) query.execute(date);
			}});
	}

	public int countAll() {
		return getJdoTemplate().execute(new JdoCallback<Integer>() {
			@Override
			public Integer doWithPersistenceManager(PersistenceManager pm) {
				Query query = pm.newQuery("select count(this) from " + Photo.class.getName());
				
				return (Integer) query.execute();
			}});
	}
}
